package com.example.stumanager.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.stumanager.entity.UserEntity;

import java.util.ArrayList;

/**
 * 用户类sql语句
 */
public class UserSql extends SQLiteOpenHelper {
    public UserSql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    //数据库初始化时用的插入表
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = "create table user(number text primary key,name text not null,passWord text not null,dormitory text not null,grade text not null,power int not null)";
        sqLiteDatabase.execSQL(sql);
    }

    private SQLiteDatabase db;

    public UserSql(Context context) {
        super(context, "Users", null, 1);
        db = this.getWritableDatabase();//we
    }

    /**
     * 添加用户
     *
     * @param u
     * @return
     */
    public boolean add(UserEntity u) {
        ContentValues values = new ContentValues();
        values.put("number", u.getNumber());
        values.put("name", u.getName());
        values.put("passWord", u.getPassWord());
        values.put("dormitory", u.getDormitory());
        values.put("grade", u.getGrade());
        values.put("power", u.getPower());
        long i = db.insert("user", null, values);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 按Id删除
     *
     * @param id
     * @return
     */
    public boolean delById(String id) {
        long i = db.delete("user", "number=?", new String[]{id});
        if (i > 0) {
            Log.d("", "删除成功");
            return true;
        }
        return false;
    }

    /**
     * 数据更新
     *
     * @param u
     * @return
     */
    public boolean change(UserEntity u) {
        ContentValues values = new ContentValues();
        if (u.getName() != null && u.getName() != "" && u.getName().length() > 0) {
            values.put("name", u.getName());
        }
        if (u.getPassWord() != null && u.getPassWord() != "" && u.getPassWord().length() > 0) {
            values.put("passWord", u.getPassWord());
        }
        if (u.getDormitory() != null && u.getDormitory() != "" && u.getDormitory().length() > 0) {
            values.put("dormitory", u.getDormitory());
        }
        if (u.getGrade() != null && u.getGrade() != "" && u.getGrade().length() > 0) {
            values.put("grade", u.getGrade());
        }
        long i = db.update("user", values, "number=?", new String[]{u.getNumber()});
        if (i > 0) {
            Log.d("", "修改成功");
            return true;
        }
        return false;
    }

    /**
     * 获取全部
     *
     * @return
     */
    public ArrayList getAll() {
        ArrayList array = new ArrayList();
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String passWord = cursor.getString(cursor.getColumnIndex("passWord"));
            @SuppressLint("Range") String dormitory = cursor.getString(cursor.getColumnIndex("dormitory"));
            @SuppressLint("Range") String grade = cursor.getString(cursor.getColumnIndex("grade"));
            UserEntity u = new UserEntity(number, name, passWord, dormitory, grade);
            array.add(u);
        }
        return array;
    }

    /**
     * 获取全部
     *
     * @return
     */
    public ArrayList getAllWhere() {
        ArrayList array = new ArrayList();
        //查询1的结果
        Cursor cursor = db.rawQuery("select * from user where power=1", null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String passWord = cursor.getString(cursor.getColumnIndex("passWord"));
            @SuppressLint("Range") String dormitory = cursor.getString(cursor.getColumnIndex("dormitory"));
            @SuppressLint("Range") String grade = cursor.getString(cursor.getColumnIndex("grade"));
            UserEntity u = new UserEntity(number, name, passWord, dormitory, grade);
            array.add(u);
        }
        return array;
    }

    /**
     * 根据学号查询
     *
     * @param numberId
     * @return
     */
    public UserEntity getById(String numberId) {
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String passWord = cursor.getString(cursor.getColumnIndex("passWord"));
            @SuppressLint("Range") String dormitory = cursor.getString(cursor.getColumnIndex("dormitory"));
            @SuppressLint("Range") String grade = cursor.getString(cursor.getColumnIndex("grade"));
            if (number.equals(numberId)) {
                UserEntity u = new UserEntity(number, name, passWord, dormitory, grade);
                return u;
            }
        }
        return null;
    }

    /**
     * 登陆判断
     *
     * @param numberId
     * @param passWordId
     * @return
     */
    public boolean check(String numberId, String passWordId) {
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String passWord = cursor.getString(cursor.getColumnIndex("passWord"));
            if (numberId.equals(number) && passWordId.equals(passWord)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 管理员登陆判断
     *
     * @param numberId
     * @param passWordId
     * @return
     */
    public boolean adminCheck(String numberId, String passWordId) {
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") Integer pow = cursor.getInt(cursor.getColumnIndex("power"));
            @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String passWord = cursor.getString(cursor.getColumnIndex("passWord"));
            if (numberId.equals(number) && passWordId.equals(passWord) && 0 == pow) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
